package _5thread;

/**
 * @Description:
 * @Author: wangzhen
 * @Time: 2021/3/30 21:27
 */
public class VolatileTest {


    public static void main(String[] args) {

        System.out.println("main-------start");

        MyThread myThread = new MyThread();
        new Thread(myThread).start();


        while (true){

            if (myThread.isFlag()){
                System.out.println("flag->"+myThread.isFlag());
                break;
            }

        }

        System.out.println("main-------end");
    }



}
class MyThread implements Runnable{

    private volatile boolean flag=false;

    public void run() {

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        flag=true;
        System.out.println("MyThread-------end");
    }

    public boolean isFlag() {

        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;

    }


}
